package com.hd123.rumba.gwt.widget2.client.grid.renderer;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.hd123.rumba.gwt.widget2.client.form.HasNumberFormat;
import com.hd123.rumba.gwt.widget2.client.grid.RGrid;
import com.hd123.rumba.gwt.widget2.client.grid.RGridColumnDef;

/**
 * 数值标签渲染器
 * 
 * @author LiQi
 * 
 */
public class RNumberRendererFactory implements RCellRendererFactory, HasNumberFormat {

  /** 数值格式 */
  private NumberFormat format;

  /**
   * @param format
   *          数值格式
   */
  public RNumberRendererFactory(NumberFormat format) {
    super();
    setFormat(format);
  }

  public RCellRenderer makeRenderer(final RGrid grid, final RGridColumnDef colDef, final int row,
      final int col) {
    Object data = grid.getProvider().getData(row - grid.getHeaderRowCount(), colDef.getIndex());
    final RNumberRenderer result = new RNumberRenderer();
    result.setFormat(format);
    result.setValue(data);

    result.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        grid.fireRGridClick(colDef, row, col, result);
      }
    });

    return result;
  }

  /**
   * 数值格式
   * 
   * @return
   */
  public NumberFormat getFormat() {
    return format;
  }

  /**
   * 设置数值格式
   * 
   * @param format
   */
  public void setFormat(NumberFormat format) {
    this.format = format;
  }

}
